package com.zw.pdm.module.report.controller;

import com.zw.pdm.base.BaseController;
import com.zw.pdm.common.JsonResult;
import com.zw.pdm.entity.rep.Sheet;
import com.zw.pdm.module.basic.service.DictCommonService;
import com.zw.pdm.module.report.service.ReportConfirmService;
import com.zw.pdm.module.report.service.SheetService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zw.pdm.constant.Constant.GROUP_CHANGE_REASON;
import static com.zw.pdm.constant.Constant.GROUP_MALTYPE;

/**
 * @Description: 计划确认接口
 *
 * @Author: China.sgl
 * @Date: 2020/8/3 16:11
 */
@Controller
@RequestMapping("/report/confirm")
@SuppressWarnings("all")
@Deprecated
public class ReportConfirmController extends BaseController {

    @Autowired
    private DictCommonService dictCommonService;
    @Autowired
    private SheetService sheetService;
    @Autowired
    private ReportConfirmService reportConfirmService;

    @GetMapping
    @RequiresPermissions({"report:confirm:view"})
    public String page(Model model) {
        model.addAttribute("malTypes", dictCommonService.findDictByGroupName(GROUP_MALTYPE));
        return "report/confirm/page";
    }

    /** 计划列表 */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"report:confirm:view"})
    public JsonResult list(Sheet sheet) {
        startPage();
        List<Sheet> list = reportConfirmService.findWithReportConfirm(sheet);
        return jsonResult(list);
    }

    /** 确认页面 */
    @GetMapping("/info/{id}")
    @RequiresPermissions({"report:confirm:view"})
    public String info(@PathVariable("id") Integer sheetId, Model model) {
        Sheet sheet = sheetService.getSheetById(sheetId);
        model.addAttribute("sheet", sheet);
        model.addAttribute("process", sheetService.findVpBySheetId(sheetId));
        if (sheet.getSheetType() == 1) {
            model.addAttribute("plans", sheetService.findPlanBySheetId(sheetId));
            return "report/confirm/confirm";
        } else {
            model.addAttribute("reasons", dictCommonService.findDictByGroupName(GROUP_CHANGE_REASON));
            model.addAttribute("changes", sheetService.findChangeBySheetId(sheetId));
            return "report/confirm/changeConfirm";
        }
    }

    /** 确认 */
    @ResponseBody
    @PostMapping("/y")
    @RequiresPermissions({"report:confirm:edit"})
    public JsonResult yes(Integer id) {
        return jsonResult(reportConfirmService.confirm(id));
    }

}
